clear all
tempfile tempsave
set seed ${seed}

/*-----------------*/
/*School Year 94/95*/
/*-----------------*/
use aid scid h1gi20 using "${temp}allwave1", clear
gen grade=h1gi20 if h1gi20<=12
recode grade (7 8=1) // All students not in HS yet
destring scid, replace
replace scid=. if scid==999
gen SchlBeg=scid 
save "${temp}year_9495.dta", replace

/*---------------*/
/*Graduation Year*/
/*---------------*/
use aid erilsaid using "${temp}edustd"	, clear
destring erilsaid, replace
rename erilsaid SchlEnd
save "${temp}year_grad.dta", replace

/*----------------------*/
/*School Characteristics*/
/*----------------------*/
	/*School Ever Assigned HS Degree?*/
use aid erilsaid using "${temp}edustd"	, clear
destring erilsaid, gen(scid)
merge 1:1 aid using "${temp}edugrad.dta", keepusing(erexitst) nogen
encode erexitst, gen(SchlHs1)
label drop _all
recode SchlHs1 1=. 4 5 6 7 8 9=0 10 2 3=1/* 0: No Graduate (special education diploma, certificate of attendance, still enrolled, drop out, other non-graduate, certificate of completion) 1: GED, High School Graduate (Diploma, Honors Degree)*/
gcollapse (max) SchlHs1, by(scid)
save  "`tempsave'", replace

	/*School Grades Covered?*/
use scid grades using "${temp}schinfo.dta", clear
destring scid, replace
merge 1:1 scid using "`tempsave'", nogen keep(match master)

	*Grades
rename grades SchlGrades 
la define grades 1 "K-12" 2 "7-8" 3 "9-12" 4 "7-9" 5 "10-12" 6 "7-12" 7 "Special" 8 "6-8" 9 "8-12" 10 "K-8" 11 "6-13" 12 "7-13" 13 "5-8" 14 "6-9" 15 "K-13" 16 "5-7"
la val SchlGrades grades

	*High School Based on Grades?
gen SchlHs2=(SchlGrades==1 | SchlGrades==3  | SchlGrades==5  | SchlGrades==6  | SchlGrades==7  | SchlGrades==9  | SchlGrades==11  | SchlGrades==12  | SchlGrades==15)

	*HS Indicator (Yes if Grade Range Correct & Ever graduated Students)
// gen SchlHs=(SchlHs1==1 & SchlHs2==1) 
gen SchlHs=(SchlHs1==1 | SchlHs2==1) 
drop SchlHs1 SchlHs2

	*Identifier for School in 94/95 and Graduation Year and ImpVar
foreach x in SchlEnd SchlBeg ImpVar{
	preserve
	gen `x'=scid
	rename (SchlGrades SchlHs) (`x'Grades `x'Hs)
	save "${temp}`x'.dta", replace
	restore
}
save "${temp}HS.dta", replace

/*-----*/
/*Merge*/
/*-----*/
use 				using "${temp}year_9495.dta", clear
merge 1:1 aid 		using "${temp}year_grad.dta", nogen
merge m:1 SchlBeg 	using "${temp}SchlBeg.dta", keep(match master) nogen
merge m:1 SchlEnd 	using "${temp}SchlEnd.dta", keep(match master) nogen

// /*-------------------*/
// /*Impute HS Indicator*/
// /*-------------------*/
gegen 	ImpVar=mode(SchlEnd) if !mi(SchlBeg), by(SchlBeg)
merge m:1 ImpVar 	using  "${temp}ImpVar.dta", keep(match master) nogen

	*HS Indicator 
	*Note: Individuals with Missing HS Indicator:
	*a) Individuals w/o School Indicator in W1
	*b) Individuals in Feeder Schools that do not cater predominantly Add Health HS
gen		HsId=SchlEnd if SchlEndHs==1
replace HsId=ImpVar if (ImpVarHs==1)

// Calculate share of people in feeder schools who transfer to their assigned high school.
gen indicator=(SchlEnd==HsId) if SchlBegHs==0 & !mi(SchlEnd)
tab indicator

	*Imputation Indicator 1 ->1 if 
	*(full sample as if behaved according to Add Health Schedule)
gen HsImp1=0 if !mi(HsId)

	*Imputation Indicator 2 -> 1 if
	*i) we know they have graduated from non Add Health HS
gen HsImp2=(SchlEnd!=HsId & !mi(SchlEnd)) if !mi(HsId)

	*Imputation Indicator 3 -> 1 if
	*i) we know they have graduated from non Add Health HS
	*ii) we are not sure whether they have graduated from non Add Health HS
gen HsImp3=(SchlEnd!=HsId) if !mi(HsId)

	*Imputation Indicator 4 -> 1 if
	*i) we know they have graduated from non Add Health HS
	*ii) we are not sure whether they have graduated from non Add Health HS
	*iii) we are not sure whether they ever entered an Add Health HS
gen HsImp4=(SchlEnd!=HsId | SchlBegHs==0) if !mi(HsId)


/*----*/
/*Save*/
/*----*/

keep aid Hs* grade
sum
save "${temp}HsId.dta", replace
